﻿#include "filtheme.h"
#include "../common/def.h"
#include "filcolors.h"
#include "../helper/filtools.h"
#include "../helper/settingshelper.h"

FilTheme::FilTheme(QObject *parent)
    : QObject{parent}
{
#if 0
    _accentColor = FilColors::getInstance()->Blue();
    _nativeText = false;
    _animationEnabled = true;
    _desktopImagePath = "";
    _blurBehindWindowEnabled = false;
    QGuiApplication::instance()->installEventFilter(this);
    refreshColors();
    connect(this, &FilTheme::accentColorChanged, this, [=] { refreshColors(); });
    connect(this, &FilTheme::blurBehindWindowEnabledChanged, this, [=] {
        checkUpdateDesktopImage();
    });
#else // use default settings
    _accentColor = FilColors::getInstance()->Blue();
    _nativeText = SettingsHelper::getInstance()->getAppNativeText();
    _animationEnabled = SettingsHelper::getInstance()->getAppAnimationEnabled();
    _desktopImagePath = "";
    _blurBehindWindowEnabled = SettingsHelper::getInstance()->getWindowBlur();
    QGuiApplication::instance()->installEventFilter(this);
    refreshColors();
    connect(this, &FilTheme::accentColorChanged, this, [=] {
        refreshColors();
    });
    connect(this, &FilTheme::blurBehindWindowEnabledChanged, this, [=] {
        SettingsHelper::getInstance()->saveWindowBlur(_blurBehindWindowEnabled);
        checkUpdateDesktopImage();
    });
    connect(this, &FilTheme::animationEnabledChanged, this, [=] {
        SettingsHelper::getInstance()->saveAppAnimationEnabled(_animationEnabled);
    });
#endif
    connect(&_watcher, &QFileSystemWatcher::fileChanged, this,
            [=](const QString &path) { Q_EMIT desktopImagePathChanged(); });
    startTimer(1000);
}

bool FilTheme::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::ApplicationPaletteChange || event->type() == QEvent::ThemeChange) {
        event->accept();
        return true;
    }
    return false;
}

void FilTheme::refreshColors()
{
    qDebug() << __func__ << _accentColor->normal().name();
    primaryColor(_accentColor->dark());
    backgroundColor(QColor(255, 255, 255, 255));
    dividerColor(QColor(210, 210, 210, 255));
    windowBackgroundColor(QColor(237, 237, 237, 255));
    windowActiveBackgroundColor(QColor(243, 243, 243, 255));
    fontPrimaryColor(QColor(7, 7, 7, 255));
    fontSecondaryColor(QColor(102, 102, 102, 255));
    fontTertiaryColor(QColor(153, 153, 153, 255));
    itemNormalColor(QColor(0, 0, 0, 0));
    frameColor(QColor(243, 243, 243, qRound(255 * 0.8)));
    frameActiveColor(QColor(255, 255, 255, qRound(255 * 0.8)));
    itemHoverColor(QColor(0, 0, 0, qRound(255 * 0.03)));
    itemPressColor(QColor(0, 0, 0, qRound(255 * 0.06)));
    itemCheckColor(QColor(0, 0, 0, qRound(255 * 0.09)));
}

void FilTheme::timerEvent(QTimerEvent *event)
{
    checkUpdateDesktopImage();
}

void FilTheme::checkUpdateDesktopImage()
{
    if (!_blurBehindWindowEnabled) {
        return ;
    }
    QThreadPool::globalInstance()->start([=]() {
        _mutex.lock();
        auto path = FilTools::getInstance()->getWallpaperFilePath();
        if (_desktopImagePath != path) {
            if (!_desktopImagePath.isEmpty()) {
                _watcher.removePath(_desktopImagePath);
            }
            desktopImagePath(path);
            _watcher.addPath(path);
        }
        _mutex.unlock();
    });
}
